package lance.honeydew.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.ItemStock;
import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.OrderInformation;
import lance.honeydew.bean.PaymentMethod;
import lance.honeydew.bean.PublishMessage;
import lance.honeydew.bean.SerialNumber;
import lance.honeydew.bean.ShoppingMoney;
import lance.honeydew.bean.TransportMethod;
import lance.honeydew.bean.UserMember;
import lance.honeydew.dao.CategoryBigDAO;
import lance.honeydew.dao.ItemInformationDAO;
import lance.honeydew.dao.ItemStockDAO;
import lance.honeydew.dao.OrderDetailDAO;
import lance.honeydew.dao.OrderInformationDAO;
import lance.honeydew.dao.PaymentMethodDAO;
import lance.honeydew.dao.PublishMessageDAO;
import lance.honeydew.dao.SerialNumberDAO;
import lance.honeydew.dao.ShoppingMoneyDAO;
import lance.honeydew.dao.TransportMethodDAO;
import lance.honeydew.dao.UserMemberDAO;
import lance.honeydew.util.DateUtil;
import lance.honeydew.util.ItemStockUtil;
import lance.honeydew.util.SerialNumberUtil;
import lance.honeydew.util.UserMemberUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class ShopService extends BaseService {

	private static final Logger logger = Logger.getLogger(ShopService.class);
	
	private PublishMessageDAO publishMessageDAO;
	
	private UserMemberDAO userMemberDao;
	
	private OrderInformationDAO orderInformationDAO;
	
	private CategoryBigDAO categoryBigDAO;
	
	private ItemInformationDAO itemInformationDAO;
	
	private ItemStockDAO itemStockDAO;
	
	private PaymentMethodDAO paymentMethodDAO;
	
	private TransportMethodDAO transportMethodDAO;
	
	private ShoppingMoneyDAO shoppingMoneyDAO;
	
	private SerialNumberDAO serialNumberDAO;
	
	private OrderDetailDAO orderDetailDAO;
	
	
	public ItemStockDAO getItemStockDAO() {
		return itemStockDAO;
	}

	public void setItemStockDAO(ItemStockDAO itemStockDAO) {
		this.itemStockDAO = itemStockDAO;
	}

	public ItemInformationDAO getItemInformationDAO() {
		return itemInformationDAO;
	}

	public void setItemInformationDAO(ItemInformationDAO itemInformationDAO) {
		this.itemInformationDAO = itemInformationDAO;
	}

	public OrderInformationDAO getOrderInformationDAO() {
		return orderInformationDAO;
	}

	public void setOrderInformationDAO(OrderInformationDAO orderInformationDAO) {
		this.orderInformationDAO = orderInformationDAO;
	}

	public UserMemberDAO getUserMemberDao() {
		return userMemberDao;
	}

	public void setUserMemberDao(UserMemberDAO userMemberDao) {
		this.userMemberDao = userMemberDao;
	}

	public PublishMessageDAO getPublishMessageDAO() {
		return publishMessageDAO;
	}

	public void setPublishMessageDAO(PublishMessageDAO publishMessageDAO) {
		this.publishMessageDAO = publishMessageDAO;
	}
	
	public CategoryBigDAO getCategoryBigDAO() {
		return categoryBigDAO;
	}

	public void setCategoryBigDAO(CategoryBigDAO categoryBigDAO) {
		this.categoryBigDAO = categoryBigDAO;
	}

	public PaymentMethodDAO getPaymentMethodDAO() {
		return paymentMethodDAO;
	}

	public void setPaymentMethodDAO(PaymentMethodDAO paymentMethodDAO) {
		this.paymentMethodDAO = paymentMethodDAO;
	}

	public TransportMethodDAO getTransportMethodDAO() {
		return transportMethodDAO;
	}

	public void setTransportMethodDAO(TransportMethodDAO transportMethodDAO) {
		this.transportMethodDAO = transportMethodDAO;
	}

	public ShoppingMoneyDAO getShoppingMoneyDAO() {
		return shoppingMoneyDAO;
	}

	public void setShoppingMoneyDAO(ShoppingMoneyDAO shoppingMoneyDAO) {
		this.shoppingMoneyDAO = shoppingMoneyDAO;
	}

	public SerialNumberDAO getSerialNumberDAO() {
		return serialNumberDAO;
	}

	public void setSerialNumberDAO(SerialNumberDAO serialNumberDAO) {
		this.serialNumberDAO = serialNumberDAO;
	}

	public OrderDetailDAO getOrderDetailDAO() {
		return orderDetailDAO;
	}

	public void setOrderDetailDAO(OrderDetailDAO orderDetailDAO) {
		this.orderDetailDAO = orderDetailDAO;
	}

	/*Method*/
	/**
	 * queryPublishMessageByExplain02
	 * @return
	 */
	public List<PublishMessage> queryPublishMessageByExplain02() {
		
		List<PublishMessage> result = null;
		try {
			
			result = publishMessageDAO.queryPublishMessageByExplain02();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
	}
	
	/**
	 * checkLoginUserMember
	 * @param email
	 * @param password
	 * @return
	 */
	public UserMember checkLoginUserMember(String email, String password) {
		
		UserMember result = null;
		
		try {
			
			UserMember user = userMemberDao.checkLoginUserMember(email, password);
			
			if (user != null) {
				if (user.getState().equals(UserMemberUtil.STATE_USERMEMBER_ENABLE_INT)
						&& (user.getIdentity().equals(UserMemberUtil.IDENTITY_GENERAL_INT)
						|| user.getIdentity().equals(UserMemberUtil.IDENTITY_VIP_INT))) {
					result = user;
				}
			} else {
				result = null;
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findUserMemberByUserId
	 * @param userId
	 * @return
	 */
	public UserMember findUserMemberByUserId(Long userId) {
		
		UserMember result = null;
		
		try {
			
			UserMember obj = new UserMember();
			obj.setUserid(userId);
			result = userMemberDao.findUserMemberByUserId(obj);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createUserMemberByObj
	 * @param obj
	 * @return
	 */
	public boolean createUserMemberByObj(UserMember obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getBrithdayStr())) {
				obj.setBrithday(DateUtil.dbStrToDate(obj.getBrithdayStr() + " 00:00:00"));
			}
			
			userMemberDao.create(obj);			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateUserMemberByObj
	 * @param obj
	 * @return
	 */
	public boolean updateUserMemberByObj(UserMember obj) {
		
		boolean result = false;
		
		try {
			
			userMemberDao.update(obj);			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findUserMemberByEmail
	 * @param email
	 * @return
	 */
	public UserMember findUserMemberByEmail(String email) {
		
		UserMember result = null;
		try {
			
			result = userMemberDao.findUserMemberByEmail(email);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findOrderInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public OrderInformation findOrderInformationBySeqId(Long seqId) {
		
		OrderInformation result = null;
		try {
			
			result = orderInformationDAO.findOrderInformationBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findAllCategoryBigList
	 * @return
	 */
	public List<CategoryBig> findAllCategoryBigList() {

		List<CategoryBig> list = null;
		
		try {
			
			list = categoryBigDAO.findAllCategoryBigList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;		
	}
	
	/**
	 * queryItemInformationByIndexQuery
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<ItemInformation> queryItemInformationByIndexQuery(Long categoryBigSeqid, Long categorymediumSeqid, int pageNum, int pageSize) {
		
		List<ItemInformation> result = null;
		
		try {
			
			result = itemInformationDAO.queryItemInformationByIndexQuery(categoryBigSeqid, categorymediumSeqid, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryItemInformationByIndexQuery
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<ItemInformation> queryItemInformationByIndexQueryAll(String itemName, Long categoryBigSeqid, Long categorymediumSeqid
			, int pageNum, int pageSize) {
		
		List<ItemInformation> result = null;
		
		try {
			
			result = itemInformationDAO.queryItemInformationByIndexQueryAll(itemName, categoryBigSeqid, categorymediumSeqid, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * countItemInformationByIndexQuery
	 * @param categoryBigSeqid
	 * @param categorymediumSeqid
	 * @return
	 */
	public int countItemInformationByIndexQuery(Long categoryBigSeqid, Long categorymediumSeqid) {
		
		int result = 0;
		try {
			
			result = itemInformationDAO.countItemInformationByIndexQuery(categoryBigSeqid, categorymediumSeqid);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
	}
	
	/**
	 * countItemInformationByIndexQueryAll
	 * @param categoryBigSeqid
	 * @param categorymediumSeqid
	 * @return
	 */
	public int countItemInformationByIndexQueryAll(String itemName, 
			Long categoryBigSeqid, Long categorymediumSeqid) {
		
		int result = 0;
		try {
			
			result = itemInformationDAO.countItemInformationByIndexQueryAll(itemName, categoryBigSeqid, categorymediumSeqid);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
	}
	
	/**
	 * findItemInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public ItemInformation findItemInformationByIndexInside(Long seqId) {
		
		ItemInformation result = null;
		
		try {
			
			result = itemInformationDAO.findItemInformationBySeqId(seqId);
			
			if (result != null) {
				result.setStartdateStr(DateUtil.dbDateToStr(result.getStartdate()));
				result.setEnddateStr(DateUtil.dbDateToStr(result.getEnddate()));
								
			}
			
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findItemStockBySeqId
	 * @param seqId
	 * @return
	 */
	public ItemStock findItemStockBySeqId(Long seqId) {
		
		ItemStock result = null;
		
		try {
			
			result = itemStockDAO.findItemStockBySeqId(seqId);
			
			if (result != null) {
				result.setStartdateStr(DateUtil.dbDateToStr(result.getStartdate()));
				result.setEnddateStr(DateUtil.dbDateToStr(result.getEnddate()));
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateItemStockByObj
	 * @param obj
	 * @return
	 */
	public boolean updateItemStockByObj(ItemStock obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getStartdateStr())) {
				obj.setStartdate(DateUtil.dbStrToDate(obj.getStartdateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getEnddateStr())) {
				obj.setEnddate(DateUtil.dbStrToDate(obj.getEnddateStr() + " 00:00:00"));
			}
			
			itemStockDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findAllTransportMethodList
	 * @return
	 */
	public List<TransportMethod> findAllTransportMethodList() {
		
		List<TransportMethod> result = null;
		
		try {
			
			result = transportMethodDAO.findAllTransportMethodList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findAllPaymentMethodList
	 * @return
	 */
	public List<PaymentMethod> findAllPaymentMethodList() {
		
		List<PaymentMethod> result = null;
		
		try {
			
			result = paymentMethodDAO.findAllPaymentMethodList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findPaymentMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public PaymentMethod findPaymentMethodBySeqId(Long seqId) {
		
		PaymentMethod result = null;
		
		try {
			
			result = paymentMethodDAO.findPaymentMethodBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findTransportMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public TransportMethod findTransportMethodBySeqId(Long seqId) {
		
		TransportMethod result = null;
		
		try {
			
			result = transportMethodDAO.findTransportMethodBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateShoppingMoneyByObj
	 * @param obj
	 * @return
	 */
	public boolean updateShoppingMoneyByObj(ShoppingMoney obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getProcessdateStr())) {
				obj.setProcessdate(DateUtil.dbStrToDate(obj.getProcessdateStr() + " 00:00:00"));
			}
			shoppingMoneyDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createOrderInformationByObj
	 * @param obj
	 * @return
	 */
	public boolean createOrderInformationByObj(OrderInformation obj) {
		
		boolean result = false;
		
		try {
			
			orderInformationDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findSerialNumberByTableName
	 * @param tableName
	 * @return
	 */
	public String generateSerialNumber(String tableName) {
		
		String result = null;
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			
			SerialNumber serialNumber = serialNumberDAO.findSerialNumberByTableName(tableName);
			
			//如果不是今天，將流水號重置
			if (sdf.format(today)
					.compareTo(sdf.format(serialNumber.getUpdateTime())) != 0) {
				
				serialNumber.setSerialnumber(new Long(1));
				serialNumber.setUpdateTime(today);
				serialNumberDAO.update(serialNumber);
			}
			
			
			result = SerialNumberUtil.generateSerialNumber(today, String.valueOf(serialNumber.getSerialnumber()), 5);
			
			//取得後+1更新資料
			serialNumber.setSerialnumber(serialNumber.getSerialnumber() + 1);
			serialNumberDAO.update(serialNumber);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createOrderDetailByObj
	 * @param obj
	 * @return
	 */
	public boolean createOrderDetailByObj(OrderDetail obj) {
		
		boolean result = false;
		try {
			
			orderDetailDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
		
	}
	
	/**
	 * findOrderInformationByNumber
	 * @param Number
	 * @return
	 */
	public OrderInformation findOrderInformationByNumber(String Number) {
		
		OrderInformation result = null;
		try {
			
			result = orderInformationDAO.findOrderInformationByNumber(Number);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
		
	}
}
